/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject;

/**
 * This class provides tools for threads management, synchronising, etc
 * 
 * @author Telem
 */
public class ThreadTools {

	/**
	 * Makes the current thread wait for a given time length, while blocking
	 * interruptions
	 * 
	 * @param timelength
	 *            The amount of time to wait before returning
	 * @return whether interrruptions have been caught during the wait
	 */
	public static boolean waitUninterrupted(long timelength) {
		boolean interrupted = false;
		long currenttime = System.currentTimeMillis();
		long waiteduntil = currenttime + timelength;
		while (waiteduntil - currenttime > 0) {
			try {
				Thread.sleep(waiteduntil - currenttime);
			} catch (InterruptedException e1) {
				interrupted = true;
			}
			currenttime = System.currentTimeMillis();
		}
		return interrupted;
	}
}
